﻿using System.Web.Mvc;
using System.Web;
using Super.Common;
using Super.DomainService;

namespace Super.Filter
{
    public class BkExceptionFilter : FilterAttribute, IExceptionFilter
    {

        public void OnException(ExceptionContext filterContext)
        {
            filterContext.ExceptionHandled = true;

            var s = SuperService.GetErrorHtml(filterContext.Exception);

            //过滤掉来路为_log的异常日志.
            if (HttpContext.Current.Request.UrlReferrer == null || HttpContext.Current.Request.UrlReferrer.LocalPath.StartsWith("/_log"))
            {
                Log.FileLog.Write(filterContext.Exception.Message, LogType.Exception, s);
            }

            filterContext.Result = new ContentResult { Content = SuperService.WebErrorPage(s) };

            //LogHelper.WriteErrorLog(filterContext.Exception.Message);
            //filterContext.ExceptionHandled = true;

            //var controller = filterContext.RouteData.Values["controller"].ToString();
            //var action = filterContext.RouteData.Values["action"].ToString();

            //filterContext.Controller.ViewData["Controller"] = controller;
            //filterContext.Controller.ViewData["Action"] = action;

            //filterContext.Controller.ViewData["ErrorInfo"] = filterContext.Exception.Message;
            //filterContext.Controller.ViewData["ErrorTrace"] = filterContext.Exception.StackTrace;
            //开发阶段暂时不输出自定义错误页面
            //filterContext.Result = new ViewResult() { ViewName = "Error", ViewData = filterContext.Controller.ViewData };
        }
    }
}
